import {
	USER_SET_USER_TOKEN,
	USER_SET_USER_INFO,
	USER_LOGOUT,
	USER_SET_AVATAR,
	USER_SET_NICKNAME,
	USER_SET_EMAIL
} from '../action-types'

const initUserInfo = {
	id: '',
	role: '',
	email: '',
	token: '',
	avatar: '',
	username: '',
	nickname: ''
}

export default function user(preState = initUserInfo, action) {
	const {type, data} = action;
	switch (type) {
		//Set user token
		case USER_SET_USER_TOKEN:
			return {...preState, token: data.token};
		//Set avatar
		case USER_SET_AVATAR:
			return {...preState, avatar: data.avatar}
		case USER_SET_NICKNAME:
			return {...preState, nickname: data.nickname}
		case USER_SET_EMAIL:
			return {...preState, email: data.email}
		case USER_SET_USER_INFO:
			return {
				...preState,
				username: data.username,
				avatar: data.avatar,
				role: data.role,
				id: data.id,
				email: data.email,
				nickname: data.nickname
			};
		case USER_LOGOUT:
			return {};
		default:
			return preState;
	}
}
